package com.zhentao.controller;


import com.alibaba.excel.EasyExcel;
import com.zhentao.aop.MyLog;
import com.zhentao.domain.SSubject;
import com.zhentao.domain.dto.SubjectDto;
import com.zhentao.domain.dto.subjectExport;
import com.zhentao.domain.excel.TeacherExport;
import com.zhentao.result.Result;
import com.zhentao.service.SLearningProgressService;
import com.zhentao.service.SQuestionService;
import com.zhentao.service.SSubjectService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/subject")
@Api(tags = "课程管理")
public class SubjectController {


    @Resource
    private SSubjectService subjectService;

    @Resource
    private SLearningProgressService sLearningProgressService;

    @Resource
    private SQuestionService questionService;

    //查找课程列表
    @PostMapping("/getSubjectList")
    public Result getSubjectList(@RequestBody SubjectDto subjectDto) {
        return subjectService.getSubjectList(subjectDto);
    }


    //获取阶段名称（专高/专业/实训）
    @PostMapping("/getLevelList")
    public Result getLevelList() {
        return subjectService.getLevelList();
    }

    //获取具体阶段名称（专业一/专业二....）
    @PostMapping("/getSLearningProgressList")
    public Result getSLearningProgressList() {
        return subjectService.getSLearningProgressList();
    }

    //新增课程列表
    @PostMapping("/addSubject")
    public Result addSubject(@RequestBody SSubject sSubject) {
        return subjectService.addSubject(sSubject);
    }



    //根据课程id查找课程
    @GetMapping("/getSubjectById/{id}")
    public Result getSubjectById(@PathVariable("id") Integer id) {
        return subjectService.getSubjectById(id);
    }

    //修改课程列表
    @PostMapping("/updateSubject")
    @MyLog(title = "课程",content = "修改课程")
    public Result updateSubject(@RequestBody SSubject sSubject) {
        return subjectService.updateSubject(sSubject);
    }


    //删除课程
    @GetMapping("/delSubject/{id}")
    @MyLog(title = "课程",content = "删除课程")
    public Result delSubject(@PathVariable("id") Integer id) {
        return subjectService.delSubject(id);
    }


    //根据阶段id查找具体专业几
    @GetMapping("/getSLevelById/{id}")
    public Result getSLearningProgress(@PathVariable("id") Integer id) {
        return sLearningProgressService.getSLevelById(id);
    }


    //导出课程excel
    @GetMapping("/exportFile")
    @MyLog(title = "课程",content = "导出课程")
    public void export(HttpServletResponse response) throws IOException {
        List<subjectExport> list = subjectService.getSubjectExport();
        response.setContentType("application/vnd.openxmlformats-officedocument,spreadsheetml,sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("全部课程", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), subjectExport.class).sheet("全部课程").doWrite(list);
    }


    //根据课程id查找对应的题目信息
    @GetMapping("/getQuestionBySubjectId/{id}")
    public Result getQuestionBySubjectId(@PathVariable("id") Integer id) {
        return questionService.getQuestionBySubjectId(id);
    }

}
